﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 天机万年历.自定义控件
{
    public struct Time
    {
        public int Hour { get; set; }
        public int Minute { get; set; }
        public int Second { get; set; }

        public Time(int sec)
        {
            Hour = 0;
            Minute = 0;
            Second = 0;
            if (sec > 86400)
            {
                throw new Exception("超一天秒数最大值86400秒");
            }
            Second = sec % 60;
            if (sec > 60)
            {
                Minute = (sec / 60) % 60;
            }
            if (sec / 60 > 60)
            {
                Hour = (sec / 60 / 60);
            }
        }
        public Time(string str)
        {
            Hour = 0;
            Minute = 0;
            Second = 0;
            var l = str.Split(':');
            if (l.Length != 3)
            {
                throw new Exception("格式错误：" + str);
            }
            Hour = int.Parse(l[0]);
            Minute = int.Parse(l[1]);
            Second = int.Parse(l[2]);
        }

        public override string ToString()
        {
            return string.Format("{0:D2}:{1:D2}:{2:D2}", Hour, Minute, Second);
        }
        public override bool Equals(object obj)
        {
            Time time2 = (Time)obj;
            return (Hour == time2.Hour && Minute == time2.Minute && Second == time2.Second);
        }

        public override int GetHashCode()
        {
            return Hour.GetHashCode() + Minute.GetHashCode() + Second.GetHashCode();
        }

        public static bool operator ==(Time time1,Time time2)
        {
            return (time1.Hour == time2.Hour && time1.Minute == time2.Minute && time1.Second == time2.Second);
        }
        public static bool operator !=(Time time1, Time time2)
        {
            return !(time1.Hour == time2.Hour && time1.Minute == time2.Minute && time1.Second == time2.Second);
        }
    }
}
